/**
 * Created by karl.zheng on 2018/1/8.
 */
const path = require('path');
const cleanWebpackPlugin = require('clean-webpack-plugin')
const htmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const chalk = require('chalk')
const yargs = require('yargs')
const envmod = require('gulp-env-modify');
const fns = require('./build/functions');
const md5 = require('md5');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

let env = envmod.getData()
let rootPath = path.join(__dirname, 'src', env.DEV_ROOT)
let { argv } = yargs

/**
 * 0: dev, 1: test, 2: prod
 */
let mode = 1 // default
if (argv.mode === "production") {
	mode = 2
}

if (argv.dev) {
	mode = 0
}

try {

	let STORE_PREFIX_SIZE = 6
	let definePlugin = {
		SERVER_ADDERSS: JSON.stringify(fns.getServerAddress(mode)),
		fbRedirectUrl: JSON.stringify(fns.getFbRedirectUrl(mode)),
		STORE_PREFIX_SIZE: STORE_PREFIX_SIZE,
		STORE_PREFIX: JSON.stringify(md5(env.DEV_ROOT).slice(0, STORE_PREFIX_SIZE)),
		IS_PROD: mode === 2,

		HIDE: JSON.stringify('hide'),
		INFO: JSON.stringify('info'),
		JOIN: JSON.stringify('join'),
		CDKEYS: JSON.stringify('cdKeys'),
		LAST: JSON.stringify('last'),
		GRAY: JSON.stringify('gray'),

	}

	let entry = {
		index: path.join(rootPath, 'script/index.ts')
	}
	if (env.HAS_MOBILE) entry.mobile = path.join(rootPath, 'script/mobile.ts')

	let webpackConfig = {

		entry: entry,

		resolve: {
			extensions: [".ts", ".tsx", ".js"],
			alias: {
				common: path.join(__dirname, 'common'),
			}
		},

		output: {
			path: path.join(__dirname, 'dist'),
			filename: '[name]-[hash:4].js',
			chunkFilename: '[name]-[hash:4].js',
			publicPath: 'http://sdk-sg.pocketgamesol.com/page/sdk-add-shortcuts'
		},

		module: {
			rules: [
				{
					test: /\.scss$/,
					use: [
						MiniCssExtractPlugin.loader,
						"css-loader",
						{
							loader: 'postcss-loader',
							options: {
								plugins: (loader) => [
									require('autoprefixer')(),
								]
							}
						},
						"sass-loader"
					]
				}, {
					test: /\.ts$/,
					use: ['ts-loader']
				}, {
					test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
					use: [{
						loader: 'file-loader',
						options: {
							name(file) {
								return '/' + file.slice(file.indexOf('images')).replace(/\\/g, '/')
							}
						}
					}]
				}
			]
		},

		plugins: [
			new webpack.DefinePlugin(definePlugin),
			new webpack.ProvidePlugin({
				$: 'jquery',
				md5: 'md5',
			}),
			new MiniCssExtractPlugin({
				filename: "[name]-[hash:4].css",
				chunkFilename: "[id]-[hash:4].css",
				outputPath: 'css'
			})
		],

		devServer: {
			contentBase: path.join(__dirname, 'dist'),
			inline: true,
			port: 8088,
		}
	}
	var minify = false

	if (argv.mode === 'production') {

		minify = {
			collapseWhitespace: true,
		}

		var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
		webpackConfig.plugins.push(
			new OptimizeCssAssetsPlugin({
				// assetNameRegExp: /\.\D\.css$/g,
				cssProcessor: require('cssnano'),
				cssProcessorOptions: { discardComments: { removeAll: true } },
				canPrint: true
			})
		)
	}

	if (mode !== 0) {
		webpackConfig.plugins.push(
			new cleanWebpackPlugin(path.join(__dirname, 'dist', '**/*'))
		)
	}

	webpackConfig.plugins.push(
		new htmlWebpackPlugin({
			filename: "index.html",
			template: path.join(rootPath, 'index.html'),
			chunks: ['index'],
			minify: minify
		})
	)


	if (env.HAS_MOBILE) {
		webpackConfig.plugins.push(
			new htmlWebpackPlugin({
				filename: "mobile/index.html",
				chunks: ['mobile'],
				template: path.join(rootPath, 'mobile.html'),
				minify: minify
			})
		)
	}

	module.exports = webpackConfig

} catch (err) {
	fns.msgHandler(err)
}





